from model import Model
from density_generator import DensityGen
from bsmart import BSmart
from random import uniform
from ghmm import HMMOpen, Alphabet, SequenceSet


#randomly generation of models
n_models = 4


alphabet=['a','b','c','d','e','f','g','h','i','l']
b = BSmart(alphabet)

for count in range(n_models):
    #Randomly generates a model:
    n_states = int(uniform(2,7))
    transitionGenerator=DensityGen()
    transitionGenerator.generate(n_states,n_states, 0,0)
    emissionGenerator=DensityGen()
    emissionGenerator.generate(n_states,10,3,3)
    startGenerator=DensityGen()
    startGenerator.generate(1, n_states, 0,0)
    aprioriGenerator=DensityGen()
    aprioriGenerator.generate(1,n_models,0,0)
    m =b.createModel(transitionGenerator.getAllProbabilities(),
                  emissionGenerator.getAllProbabilities() ,
                  startGenerator.getProbability(0))
    b.addModel(m);
    b.addModelProbability(count, aprioriGenerator.getProbability(0)[count] )




"""
b=BSmart(['a','b'])
#b.setAlphabet(['a','b','c','d'])
observations = []
observations.append(['a','b','a', 'b'])
observations.append(['a','b'])
#observations.append(['c','a','b','d','c'])

b.createInitialModelPool(observations)
model=b.MergeModels(b.getModel(0)[0],b.getModel(1)[0], 0,0)
b2=BSmart(['a','b'])
b2.addModel(model)
model2=b.MergeStates(b.getModel(2)[0], 0,4)
"""

"""
#prepatation phase
l=['a','b','c','d','e','f','g','h','i','l']
a=Alphabet(l)
sequences=SequenceSetOpen(a, "./models/2500_random_observations.txt")
seqList=[]
for s in sequences[0]:
    seqList.append(map(a.external, s))

#preparation end
bs=BSmart(l)
bs.learn(seqList, 4, 100)
"""




"""
#save some generated sequences...
l=['a','b','c','d','e','f','g','h','i','l']
a=Alphabet(l)
sequences_for_model = 50
m0=HMMOpen("./models/model0.xml")
m1=HMMOpen("./models/model1.xml")
m2=HMMOpen("./models/model2.xml")
m3=HMMOpen("./models/model3.xml")
probFile = file("./models/probabilities.txt", 'r')
lines = probFile.readlines()
prob = []
for line in lines:
    prob.append(float(line.split()[1]))
probFile.close()
bs = BSmart(l)
bs.addModel(m0)
bs.addModel(m1)
bs.addModel(m2)
bs.addModel(m3)
for i in range(len(prob)):
    bs.addModelProbability(i, prob[i])   
seqM0= []
for i in range(sequences_for_model):
    seqM0.append(map(a.external, m0.sampleSingle(int(uniform(10,50)))))

seqM1= []
for i in range(sequences_for_model):
    seqM1.append(map(a.external, m1.sampleSingle(int(uniform(10,50)))))

seqM2= []
for i in range(sequences_for_model):
    seqM2.append(map(a.external, m2.sampleSingle(int(uniform(10,50)))))

seqM3= []
for i in range(sequences_for_model):
    seqM3.append(map(a.external, m3.sampleSingle(int(uniform(10,50)))))

s0=SequenceSet(a,seqM0)
s1=SequenceSet(a,seqM1)
s2=SequenceSet(a,seqM2)
s3=SequenceSet(a,seqM3)
s0.write("./models/sequencesStep1.txt")
s1.write("./models/sequencesStep1.txt")
s2.write("./models/sequencesStep1.txt")
s3.write("./models/sequencesStep1.txt")



seqAll= [[],[],[],[]]
for i in range(sequences_for_model*8):
    ret = bs.produceVerbose(int(uniform(10,50)))
    seqAll[ret[1]].append(ret[0])


for i in seqAll:
    SequenceSet(a,i).write("./models/sequencesStep2.txt")
"""
